package com.bing.services.impl;

import com.bing.Component.Context;
import com.bing.bean.weixin.Command;
import com.bing.bean.weixin.ResponseMessage;
import com.bing.utils.MsgHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * Created by Administrator on 2014/12/13.
 */
public class DBCommandServiceImpl extends AbstractCommandService {
    protected int order = 5;


    private static Logger logger = LoggerFactory.getLogger(DBCommandServiceImpl.class);

    @Override
    public ResponseMessage handle(Context context) {
        logger.debug("DB 处理");
        if (context == null || StringUtils.isEmpty(context.getCommand())) {
            return null;
        }
        ResponseMessage responseMessage = null;
        List<Command> commands = context.getAccount().getCommands();
        for (Command command : commands) {
            if (command.getCommand().equals(context.getCommand())) {
                responseMessage = command.getResponseMessage();
                break;
            }
        }
        MsgHelper.toMsg(responseMessage, context.getRequestMessage());
        return responseMessage;
    }

    @Override
    protected List<String> getContexts() {
        return null;
    }

    @Override
    public boolean canHandle(Context context) {
        List<Command> commands = context.getAccount().getCommands();
        for (Command command : commands) {
            if (command.getCommand().equals(context.getCommand())) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected Logger getLogger() {
        return null;
    }

    @Override
    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
}
